package com.jhy.lads.test;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description:
 * @authoer:jihaiyu
 * @date: 2017/6/13 14:35
 */
public class BadSuspend {
    public static Object u = new Object();
    static ChangeObjectThread t1 = new ChangeObjectThread("t1");
    static ChangeObjectThread t2 = new ChangeObjectThread("t2");
    private static Lock accountLock = new ReentrantLock();
    private static Semaphore semaphore = new Semaphore(4);
    public static class ChangeObjectThread extends Thread {
        public ChangeObjectThread(String name){
            super.setName(name);
        }
        @Override
        public void run() {
            accountLock.lock();
            try {
                semaphore.acquire();
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (u) {
                System.out.println("in "+getName());
                Thread.currentThread().suspend();

            }
            semaphore.release();
            accountLock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        t1.start();
        Thread.sleep(100);
        t2.start();
        t1.resume();
        t2.resume();
        t1.join();
        t2.join();
        t1.yield();

    }
}
